package com.util;

import java.util.Random;
import java.util.UUID;

/**
 * @Description: 创建UUID作为主键
 * @Project：admin
 * @Author : shanzhitong
 * @Date ： 2017-08-17
 * @version 1.0
 */
public class UUIDUtil {
    public static String getUUID() {
        String s = UUID.randomUUID().toString();
        String[] sArr = s.split("");
        StringBuilder sb = new StringBuilder();
        for (String str : sArr) {
            if ("-".equals(str)) {
                str = getRandomString(1);
            }
            sb.append(str);
        }
        // 去掉“-”符号
        return sb.toString();
    }

    public static String getRandomString(int length) { // length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
